﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Data;
using System.Linq;
using System.Transactions;
using System.Web.Hosting;
using System.Data.Objects;

namespace PAMD.Database.Services
{
    [Export(typeof(ProfileService))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class ProfileService
    {
        readonly PAMDEntities _contexts = new PAMDEntities();

        public ProfileService()
        {
            _contexts.ListingFeatures.MergeOption = MergeOption.OverwriteChanges;
            _contexts.ListingPhotoes.MergeOption = MergeOption.OverwriteChanges;
            _contexts.ListingPrices.MergeOption = MergeOption.OverwriteChanges;
            _contexts.Listings.MergeOption = MergeOption.OverwriteChanges;
            _contexts.Occupations.MergeOption = MergeOption.OverwriteChanges;
            _contexts.Registrations.MergeOption = MergeOption.OverwriteChanges;
            _contexts.UserDetails.MergeOption = MergeOption.OverwriteChanges;
            _contexts.Addresses.MergeOption = MergeOption.OverwriteChanges;
            _contexts.Countries.MergeOption = MergeOption.OverwriteChanges;
        }

        public UserDetail GetByUserName(string username)
        {
            var loweUserName = username.Trim().ToLower();
            bool found = _contexts.UserDetails.Any(u => u.Email == loweUserName);
            if (found)
            {
                return _contexts.UserDetails.First(u => u.Email == loweUserName);
            }
            return null;
        }

        public bool UpdateUser(UserDetail user, Address address)
        {
            var userDetail = GetByUserName(user.Email);

            using (TransactionScope scope = new TransactionScope())
            {
                if (userDetail.AddressID != 0)
                {
                    var addr = _contexts.Addresses.First(a => a.ID == userDetail.AddressID);
                    addr.Line1 = address.Line1;
                    addr.Line2 = address.Line2;
                    addr.Line3 = address.Line3;
                    addr.PostCode = address.PostCode;

                    //copy id from database
                    address.ID = addr.ID;
                }
                else
                {
                    address.UserID = userDetail.ID;
                    address.AddressType = "Permanent";
                    _contexts.AddToAddresses(address);
                }
                _contexts.SaveChanges();

                //update correct address id
                userDetail.AddressID = address.ID;

                userDetail.ContactNumber = user.ContactNumber;
                userDetail.Country = user.Country;
                userDetail.DateOfBirth = user.DateOfBirth;
                userDetail.Mobile = user.Mobile;
                userDetail.Occupation = user.Occupation;
                _contexts.SaveChanges();

                scope.Complete();
            }
            return true;
        }

        public Address GetAddreess(int addressId)
        {
            bool found = _contexts.Addresses.Any(a => a.ID == addressId);
            if (found)
            {
                return _contexts.Addresses.First(a => a.ID == addressId);
            }

            return null;
        }
    }
}